import express from 'express'
import { createServer } from 'http'
import { Server } from 'socket.io'
import { chatService } from './services/chat/ChatService'
import { gameService } from './services/game/GameService'
import { giftService } from './services/gift/GiftService'

const app = express()
const httpServer = createServer(app)
const io = new Server(httpServer)

// Socket.IO 连接处理
io.on('connection', (socket) => {
  // 聊天服务
  socket.on('chat:message', (data) => chatService.handleMessage(socket, data))

  // 游戏服务
  socket.on('game:join', (data) => gameService.handleJoin(socket, data))
  socket.on('game:action', (data) => gameService.handleAction(socket, data))

  // 礼物服务
  socket.on('gift:send', (data) => giftService.handleGift(socket, data))
})

// API 路由
app.use('/api/live', require('./routes/live'))
app.use('/api/game', require('./routes/game'))
app.use('/api/user', require('./routes/user'))

export default httpServer
